package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ex.CustomException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    /**
     * 根据ID删除部门
     * @param id 部门ID
     */
    @Override
    public void deleteById(Integer id) {
        Long deptId = Long.valueOf(id);
        Long count;
        // 查询该部门下是否存在员工
        LambdaQueryWrapper<Emp> empLambdaQueryWrapper = new LambdaQueryWrapper<Emp>()
                .eq(Emp::getDeptId, deptId);
        count = empMapper.selectCount(empLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("该部门下存在员工，请先删除该部门下的员工");
        }
        this.removeById(id);
    }
}